<?php

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use models\Basket;
use utils\out;

/**菜篮首页*/
$app->get('/personal/basket', function (Application $app) {
    return $app['twig']->render('basket.html');
});

/**添加到菜篮*/
$app->post('/personal/basket', function (Application $app, Request $request) {
    $sid = $request->get('sid');
    $uid = $request->get('uid');
    $name = $request->get('name');
    $type = $request->get('type');
    $extra = $request->get('extra');

    $baskets = Basket::getByUidTypeSid($app['db'], $uid, $type, $sid);
    if(count($baskets) == 0) {
        // 添加到菜篮子
        $array = explode(",", $name);
        for($i=0; $i<count($array); $i++) {
            Basket::add($app['db'], $uid, $type, $sid, $array[$i], $extra);
        }
    } else {
        // 从菜篮子中移除
        Basket::remove($app['db'], $uid, $type, $sid);
    }

    return $app->json(array("success"=>true, "result"=> "ok"));
});


/**我的菜篮*/
$app->get('/personal/basket/user/{uid}', function (Application $app, $uid) {
    $baskets = Basket::getByUid($app['db'], $uid);
    // if(count($baskets) == 0) {
    //     for($i=0; $i<count($baskets); $i++) {
    //         $basket = $baskets[$i];
    //     }
    // }
    return $app->json(array("success"=>true, "result"=> $baskets));
});

/**我的菜篮数量*/
$app->get('/personal/basket/count/{uid}', function (Application $app, $uid) {
    $count = Basket::countByUid($app['db'], $uid);
    return $app->json(array("success"=>true, "result"=> $count));
});


/**根据sourceId， type， openid，获取菜篮*/
$app->get('/personal/basket/user/{uid}/type/{type}/sourceId/{sourceId}', function (Application $app, $uid, $type, $sourceId) {

    $baskets = Basket::getByUidTypeSid($app['db'], $uid, $type, $sourceId);

    return $app->json(array("success"=>true, "result"=> $baskets));
});


/**更改isChecked*/
$app->post('/personal/basket/{id}/isChecked', function (Application $app, $id) {
    $basket = Basket::getById($app['db'], $id);
    if($basket) {
        if($basket['isChecked'] == 1) {
            Basket::check($app['db'], $id*1, 0);
        } else {
            Basket::check($app['db'], $id*1, 1);
        }
        
        return $app->json(array("success"=>true, "result"=> "ok"));
    } else {
        return $app->json(array("success"=>false, "result"=> "没有找到对应的ID"));
    }
});


/**批量更改isChecked*/
$app->post('/personal/basket/type/{type}/sid/{sid}/isChecked/{checked}', function (Application $app, $type, $sid, $checked) {

    Basket::checkByTypeSid($app['db'], $type, $sid, $checked);
    return $app->json(array("success"=>true, "result"=> "ok"));
});


/**删除*/
$app->delete('/personal/basket/type/{type}/sid/{sid}', function (Application $app, $type, $sid) {
    Basket::deleteByTypeSid($app['db'], $type, $sid);
    return $app->json(array("success"=>true, "result"=> "ok"));
});

/**删除*/
$app->delete('/personal/basket/uid/{uid}', function (Application $app, $uid) {
    Basket::deleteByUid($app['db'], $uid);
    return $app->json(array("success"=>true, "result"=> "ok"));
});